﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public static class EventMgr
{
    private static Dictionary<string, Action<object, EventArgs>> eventDic = new Dictionary<string, Action<object, EventArgs>>();

    public static void Register(string name, Action<object, EventArgs> handler = null)
    {
        if (eventDic.ContainsKey(name))
        {
            eventDic[name] += handler;
        }
        else
        {
            eventDic.Add(name, handler);
        }
    }
    
    public static void UnRegister(string name, Action<object, EventArgs> handler)
    {
        if (eventDic.ContainsKey(name))
        {
            eventDic[name] -= handler;
        }
    }

    public static void Call(string name, object obj = null, EventArgs args = null)
    {
        if (eventDic.ContainsKey(name))
        {
            if (eventDic != null)
            {
                eventDic[name](obj, args);
            }
        }
        else
        {
            Debug.LogWarning("no event as " + name);
        }
    }
    
    public static void Clear(string name)
    {
        eventDic.Remove(name);
    }

    public static void ClearAll()
    {
        eventDic.Clear();
    }
}
